﻿#include <QFile>
#include <QMessageBox>
#include "CRobTemplateMngDlg.h"
#include "CHeader.h"
#include "CSqlOper.h"
#include "CTemplateOperDlg.h"
#include "CLogOper.h"

CRobTemplateMngDlg::CRobTemplateMngDlg(QString sSceneGuid, CRBaseData* pDataBase, QWidget *parent)
	:m_sGuid(sSceneGuid), m_pBasicData(pDataBase), QDialog(parent)
{
	ui.setupUi(this);
	setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
	setStyleSheetFile();
	initUI();
	initConnect();
	queryTemplate();
}

CRobTemplateMngDlg::~CRobTemplateMngDlg()
{}

void CRobTemplateMngDlg::setStyleSheetFile()
{
	//设置对话框标题
	this->setWindowTitle(A2T("模版管理"));
	QString sQss = setUiStyle("page");
	setStyleSheet(sQss);
}

void CRobTemplateMngDlg::initConnect()
{
	connect(ui.widget->ui.tableView, &QTableView::doubleClicked, this, &CRobTemplateMngDlg::slotPushButtonModifyClicked);
	connect(ui.pushButtonAdd, &QPushButton::clicked, this, &CRobTemplateMngDlg::slotPushButtonAddClicked);
	connect(ui.pushButtonModify, &QPushButton::clicked, this, &CRobTemplateMngDlg::slotPushButtonModifyClicked);
	connect(ui.pushButtonDelete, &QPushButton::clicked, this, &CRobTemplateMngDlg::slotPushButtonDeleteClicked);
	connect(ui.pushButtonClose, &QPushButton::clicked, this, &CRobTemplateMngDlg::slotClose);
}

void CRobTemplateMngDlg::initUI()
{
	m_mapActOrder.insert(0, A2T("顺序执行"));
	m_mapActOrder.insert(1, A2T("倒序执行"));
	m_mapActOrder.insert(2, A2T("双向执行"));
	m_mapActOrder.insert(3, A2T("倒序双向执行"));
	QStringList headerListAction;		//水平表头
	headerListAction << A2T("序号") <<A2T("模版Guid")<< A2T("模版名称") << A2T("巡检项数量")  << A2T("巡检速度")<<A2T("执行顺序")<< A2T("Z轴速度")<< A2T("更新时间");

	QList<quint16> listColumnWidth;		//列宽
	listColumnWidth << 100 << 100 << 200 << 100 << 100 << 100 << 100 <<100 ;
	QList<quint16> listColumnHidden;	//隐藏列
	listColumnHidden.append(1);
	//设置列数
	ui.widget->m_pModel->setColumnCount(headerListAction.count());
	//设置表头
	ui.widget->m_pModel->setHorizontalHeaderLabels(headerListAction);
	ui.widget->ui.tableView->setModel(ui.widget->m_pModel);
	//设置列宽
	for (int i = 0; i < listColumnWidth.count(); i++)
	{
		ui.widget->ui.tableView->setColumnWidth(i, listColumnWidth.at(i));
	}
	//隐藏列
	if (0 < listColumnHidden.count())
	{
		for (quint16 i = 0; i < listColumnHidden.count(); i++)
		{
			ui.widget->ui.tableView->hideColumn(listColumnHidden.at(i));
		}
	}
}

//查询
void CRobTemplateMngDlg::queryTemplate()
{
	//首先清空列表
	ui.widget->m_pModel->removeRows(0, ui.widget->m_pModel->rowCount());

	QMap<QString, CTemplateInfo*> mapTemplateInfo;
	QString sErrMsg = "";
	bool bRet = selectTemplateBySceneID(m_sGuid, mapTemplateInfo, sErrMsg);
	if (bRet == false)
	{
		return;
	}
	int i = 0;
	if (mapTemplateInfo.size() > 0)
	{
		for (QMap<QString, CTemplateInfo*>::iterator itor = mapTemplateInfo.begin(); itor != mapTemplateInfo.end(); itor++)
		{
			CTemplateInfo* pTemplate = itor.value();
			if (pTemplate == nullptr)
			{
				continue;
			}
			QList<QStandardItem*> item;
			item.append(new QStandardItem(QString::number(i)));//序号
			item.append(new QStandardItem(pTemplate->m_sTemplateFixedID));//模版Guid
			item.append(new QStandardItem(pTemplate->m_sTemplateName));//模版名称
			item.append(new QStandardItem(QString::number(pTemplate->m_nItemCount)));//巡检项数量
			item.append(new QStandardItem(QString::number(pTemplate->m_dPollSpeed)));//执行速度
			item.append(new QStandardItem(m_mapActOrder.value(pTemplate->m_nActOrder)));//执行顺序
			item.append(new QStandardItem(QString::number(pTemplate->m_dSpeedZ)));//z轴速度
			item.append(new QStandardItem(pTemplate->m_sUpdateTime));//更新时间
			i++;
			//设置居中显示
			for (int nRow = 0; nRow < item.count(); nRow++)
			{
				item.at(nRow)->setTextAlignment(Qt::AlignCenter);
				item.at(nRow)->setForeground(QBrush(QColor(255, 255, 255)));
			}
			ui.widget->m_pModel->insertRow(ui.widget->m_pModel->rowCount(), item);
		}
	}
}

//添加
void CRobTemplateMngDlg::slotPushButtonAddClicked()
{
	CTemplateOperDlg oper(m_sGuid,0,m_pBasicData);//默认添加的时候是顺序执行
	oper.m_nType = 0;//添加
	oper.exec();
	queryTemplate();
}

//修改
void CRobTemplateMngDlg::slotPushButtonModifyClicked()
{
	int nRow = ui.widget->ui.tableView->currentIndex().row();
	if (nRow < 0)
	{
		messageBox(0, A2T("请选择一个记录再进行操作"));
		return;
	}
	QString sTemplateFixedId = ui.widget->m_pModel->item(nRow, 1)->text();
	int nActOrder=m_mapActOrder.key(ui.widget->m_pModel->item(nRow, 5)->text());
	CTemplateOperDlg oper(m_sGuid, nActOrder, m_pBasicData);
	oper.m_sTemplateFixGuid = sTemplateFixedId;
	oper.m_nType = 1;//修改
	oper.refushDataByTemplateID(sTemplateFixedId);
	oper.exec();
	queryTemplate();
}

//删除
void CRobTemplateMngDlg::slotPushButtonDeleteClicked()
{
	int nRow = ui.widget->ui.tableView->currentIndex().row();
	if (nRow < 0)
	{
		messageBox(0, A2T("请选择一个记录再进行操作"));
		return;
	}
	//提醒一下
	int nRet = messageBox(3, A2T("是否确定删除此模版"));
	if (QMessageBox::No == nRet || QMessageBox::Close == nRet)
	{
		return;
	}
	
	QString sTemplateFixedId = ui.widget->m_pModel->item(nRow, 1)->text();
	QString sName = ui.widget->m_pModel->item(nRow, 2)->text();
	QString sErrMsg = "";
	deleteTemplateByFixedGuid(sTemplateFixedId, sErrMsg);//执行删除模版函数

	SExecLog sLog;
	sLog.m_nAlarmLevel = enWorkInfo;
	sLog.m_sDescriptionl = sName;
	/*sLog.m_sDescriptionl = A2T("%1删除了%2名为%3的模版")
		.arg(CPublicData::instance().m_userInfo.sUserName)
		.arg(CPublicData::instance().m_mapIFScene[m_sGuid]->m_sSceneName.get())
		.arg(sName);*/
	sLog.m_sFuncName = A2T("模版");
	CLogOper::instance().addMsg(sLog,m_sGuid);

	//刷新界面
	queryTemplate();
	messageBox(0, A2T("删除成功"));
}

//关闭对话框
void CRobTemplateMngDlg::slotClose()
{
	this->close();
}
